#!/usr/bin/perl

use strict;
use File::Path;

# we define our own versions of these functions and need to call through to the underlying functions in Heimdal/GSS
my @rewritesyms = (
    "krb5_cc_end_seq_get",
    "krb5_config_get_string",
    "krb5_set_default_in_tkt_etypes",
    "krb5_get_pw_salt",
    "krb5_free_salt",
    "krb5_string_to_key_data_salt",
    "krb5_free_keyblock_contents",
    "krb5_set_real_time",
    "krb5_mk_req_extended",
    "krb5_free_keyblock",
    "krb5_auth_con_getremotesubkey",
    "krb5_auth_con_getlocalsubkey",
    "krb5_set_password",
    "krb5_set_password_using_ccache",
    "krb5_realm_compare",
    "krb5_get_renewed_creds",
    "krb5_get_validated_creds",
    "krb5_get_init_creds_keytab",
    "krb5_prompter_posix",
    "krb5_string_to_deltat",
    "krb5_get_all_client_addrs",
    "krb5_kt_get_type",
    "krb5_kt_add_entry",
    "krb5_kt_remove_entry",
    "krb5_mk_req",
    "krb5_kt_get_name",
    "krb5_rd_req",
    "krb5_free_ticket",
    "krb5_build_principal_va",
    "krb5_build_principal_va_ext",
    "krb5_cc_cache_match",
    "krb5_cc_close",
    "krb5_cc_default",
    "krb5_cc_get_config",
    "krb5_cc_get_full_name",
    "krb5_cc_get_name",
    "krb5_cc_get_principal",
    "krb5_cc_get_type",
    "krb5_cc_initialize",
    "krb5_cc_move",
    "krb5_cc_new_unique",
    "krb5_cc_resolve",
    "krb5_cc_store_cred",
    "krb5_cc_switch",
    "krb5_cc_retrieve_cred",
    "krb5_cc_remove_cred",
    "krb5_cc_get_kdc_offset",
    "krb5_cc_set_kdc_offset",
    "krb5_cc_next_cred",
    "krb5_cccol_last_change_time",
    "krb5_crypto_init",
    "krb5_crypto_getblocksize",
    "krb5_crypto_destroy",
    "krb5_decrypt_ivec",
    "krb5_encrypt_ivec",
    "krb5_crypto_getenctype",
    "krb5_generate_random_keyblock",
    "krb5_get_wrapped_length",
    "krb5_copy_creds_contents",
    "krb5_copy_data",
    "krb5_copy_principal",
    "krb5_data_copy",
    "krb5_data_free",
    "krb5_data_zero",
    "krb5_free_context",
    "krb5_free_cred_contents",
    "krb5_free_creds",
    "krb5_free_principal",
    "krb5_sname_to_principal",
    "krb5_get_credentials",
    "krb5_get_error_string",
    "krb5_get_default_principal",
    "krb5_get_error_message",
    "krb5_get_init_creds_opt_alloc",
    "krb5_get_init_creds_opt_free",
    "krb5_get_init_creds_opt_set_canonicalize",
    "krb5_get_init_creds_opt_set_forwardable",
    "krb5_get_init_creds_opt_set_proxiable",
    "krb5_get_init_creds_opt_set_renew_life",
    "krb5_get_init_creds_opt_set_tkt_life",
    "krb5_get_init_creds_password",
    "krb5_get_kdc_cred",
    "krb5_get_kdc_sec_offset",
    "krb5_init_context",
    "krb5_init_context_flags",
    "krb5_make_principal",
    "krb5_parse_name",
    "krb5_parse_name_flags",
    "krb5_principal_compare",
    "krb5_principal_get_realm",
    "krb5_timeofday",
    "krb5_unparse_name",
    "krb5_us_timeofday",
    "krb5_kt_start_seq_get",
    "krb5_kt_end_seq_get",
    "krb5_xfree",
    "krb5_kt_next_entry",
    "krb5_kt_free_entry",
    "gsskrb5_extract_authz_data_from_sec_context",
    "krb5_sendauth",
    "krb5_free_ap_rep_enc_part",
    "krb5_free_error",
    "krb5_recvauth",
    "krb5_recvauth_match_version",
    "krb5_mk_priv",
    "krb5_rd_priv",
    "krb5_mk_safe",
    "krb5_rd_safe",
    "krb5_set_home_dir_access",
    "krb5_verify_init_creds",
    "krb5_verify_init_creds_opt_init",
    "krb5_verify_init_creds_opt_set_ap_req_nofail",
    "krb5_kuserok",
    "com_right",
    "com_right_r",
    "gss_import_name",
    "krb5_appdefault_boolean",
    "krb5_appdefault_string",
    );

rmtree("stubs");
mkdir("stubs");

my $regenComment = "generated file, do not edit; re-run gen.pl to regenerate";

# indirect symbols that will be imported from Heimdal/GSS, exported from libHeimdalProxy.dylib, and called from Kerberos.framework
open my $f, ">", "stubs/reexport-stubs.txt";
print $f "# $regenComment\n";
foreach my $sym (sort(@rewritesyms)) {
    print $f "_$sym _heim_$sym\n";
}
close $f;
